from django.shortcuts import render

# Create your views here.
from rest_framework.response import Response
from rest_framework.views import APIView

from myapp.Ser import NewsModelSerializer
from myapp.models import News


class NewsAPI(APIView):
    def get(self,request):
        id = request.GET.get('id')
        if id:
            data = News.objects.get(pk=id)
            data.couter += 1
            data.save()
            data1 = NewsModelSerializer(data,many=True)
            return Response(data1.data)
        else:
            data = News.objects.all()
            data = NewsModelSerializer(data,many=True)
            return Response(data.data)

    def post(self,request):
        data1 = NewsModelSerializer(data=request.data)
        if data1.is_valid():
            data1.save()
            return Response({'code':200})